let _ = require('lodash');
const YCMJ_OP_TYPE = require('../../consts/consts').YCMJ_OP_TYPE;
const YCMJ_HUPAI_TYPE = require('../../consts/consts').YCMJ_HUPAI_TYPE;
// 所有牌
const CARDS = [
    11, 12, 13, 14, 15, 16, 17, 18, 19,			// 万
    21, 22, 23, 24, 25, 26, 27, 28, 29,			// 筒
    31, 32, 33, 34, 35, 36, 37, 38, 39,			// 条
    41, 42, 43, 44,								// 风
    51, 52, 53,								    // 字
    11, 12, 13, 14, 15, 16, 17, 18, 19,			// 万
    21, 22, 23, 24, 25, 26, 27, 28, 29,			// 筒
    31, 32, 33, 34, 35, 36, 37, 38, 39,			// 条
    41, 42, 43, 44,								// 风
    51, 52, 53,								    // 字
    11, 12, 13, 14, 15, 16, 17, 18, 19,			// 万
    21, 22, 23, 24, 25, 26, 27, 28, 29,			// 筒
    31, 32, 33, 34, 35, 36, 37, 38, 39,			// 条
    41, 42, 43, 44,								// 风
    51, 52, 53,								    // 字
    11, 12, 13, 14, 15, 16, 17, 18, 19,			// 万
    21, 22, 23, 24, 25, 26, 27, 28, 29,			// 筒
    31, 32, 33, 34, 35, 36, 37, 38, 39,			// 条
    41, 42, 43, 44,								// 风
    51, 52, 53								    // 字
   // 61, 62, 63, 64, 65, 66, 67, 68              // 花   
];

let MJLogic = function(){
    
    
};
MJLogic.prototype.shuffle = function(){
    let cards = _.shuffle(CARDS);
    return cards;
}
MJLogic.prototype.getCardType = function(card){
    return Math.floor(card / 10);
}
MJLogic.prototype.getCardValue = function(card){
    return card % 10;
}
MJLogic.prototype.isBtTwo = function(cards,type){
    for(let i =  0 ;i <  cards.length - 1; i++){
        let card = cards[i];
        if(this.getCardType(card) != type){
            return false;
        };
        let cardNext = cards[i + 1];
        if(this.getCardType(cardNext) != type){
            return false;
        };
        if(this.getCardValue(cardNext) - this.getCardValue(card) < 2){
            return false;
        };
    };
    return true;
}
MJLogic.prototype.pinghu = function(cards){
    if(cards instanceof Array &&  cards.length > 1 && (cards.length - 2) % 3 == 0){
        for(let i=0;i<cards.length-1;i++){
            if(cards[i] === cards[i+1]){
                let clone_card = _.cloneDeep(cards);
                clone_card.splice(i,2);
                if(this.isShunKe(clone_card)){
                    return true;
                };
            }
        }
        return false;
    }else
    {
        return false;
    }
}
MJLogic.prototype.isShunKe = function(clone_card){
    if(clone_card instanceof Array && clone_card.length > 0 && clone_card.length % 3 == 0){
        for(let i=0;i<clone_card.length;i +=3){
            let cards = clone_card.slice(i,i+3);
            if(!this.isShun(cards) && !this.isKe(cards)){
                return false;
            };
        };
        return true;
    }else{
        return false;
    }
}
MJLogic.prototype.isShun = function(cards){
    if(this.isAllFeng(cards)){
        for(let i=0;i<cards.length -1;i++){
            if(cards[i]  == cards[i + 1]){
                return false;
            }
        };
        return true;
    }else
    {
        for(let i=0;i<cards.length -1;i++){
            if(cards[i] + 1 != cards[i + 1]){
                return false;
            }
        }
        return true;
    };
}
MJLogic.prototype.isAllFeng = function(cards){
    for(let i=0;i<cards.length ;i++){
        if(this.getCardType(cards[i]) != 4){
            return false;
        }
    }
    return true;
}
MJLogic.prototype.isKe = function(cards){
    for(let i=0;i<cards.length-1;i++){
        if(cards[i] != cards[i + 1]){
            return false;
        };
    };
    return true;
}
MJLogic.prototype.sort = function(handCard){
    handCard.sort((a,b)=>{
        if(this.getCardType(a) == this.getCardType(b)){
            return this.getCardValue(a) - this.getCardValue(b);
        }else{
            return this.getCardType(a) - this.getCardType(b);
        };
    });
    return handCard;
}
MJLogic.prototype.peng = function(handCard,card){
    if(handCard instanceof Array &&  handCard.length >= 2 ){
        handCard.sort((a,b)=>{
            if(this.getCardType(a) == this.getCardType(b)){
                return this.getCardValue(a) - this.getCardValue(b);
            }else{
                return this.getCardType(a) - this.getCardType(b);
            }
        });
        let cards = [];
        for(let i = 0 ;i < handCard.length -1;i ++){
            if(handCard[i] === handCard[i + 1] && handCard[i]=== card){
                cards.push({
                    type:YCMJ_OP_TYPE.PENG,
                    cards:handCard[i]
                });
                i++;
                break;
            };
        };
        return cards;
    }else
    {
        return [];
    }
}
MJLogic.prototype.chi = function(handCard,card){
    if(handCard instanceof Array &&  handCard.length >= 2 ){
        let cards = [];
        for(let i=0;i<handCard.length - 1;i++){
            if(this.getCardType(handCard[i]) == 4 && this.getCardType(handCard[i+1]) == 4 && this.getCardType(card) == 4){
                if(handCard[i] != card && handCard[i+1] != card ){
                    cards.push({
                        type:YCMJ_OP_TYPE.ZHONG_CHI,
                        card:card,
                        cards:[handCard[i],handCard[i+1]]
                    });
                };
            }else
            {
                if(handCard[i] + 1 == handCard[i+1]){
                    if(handCard[i] - 1 == card){
                        cards.push({
                            type:YCMJ_OP_TYPE.ZUO_CHI,
                            card:card,
                            cards:[handCard[i],handCard[i+1]]
                        });
                    }else if(handCard[i+1] + 1 == card){
                        cards.push({
                            type:YCMJ_OP_TYPE.YOU_CHI,
                            card:card,
                            cards:[handCard[i],handCard[i+1]]
                        });
                    };
                }else if(handCard[i] + 2 == handCard[i+1]){
                    if(handCard[i] + 1 == card && card == handCard[i+1] -1){
                        cards.push({
                            type:YCMJ_OP_TYPE.ZHONG_CHI,
                            card:card,
                            cards:[handCard[i],handCard[i+1]]
                        });
                    };
                };
            }
        };
        return cards;
    }else
    {
        return [];
    }
}
MJLogic.prototype.mgang = function(handCard,card){
    if(handCard instanceof Array &&  handCard.length >= 2 ){
        handCard.sort((a,b)=>{
            if(this.getCardType(a) == this.getCardType(b)){
                return this.getCardValue(a) - this.getCardValue(b);
            }else{
                return this.getCardType(a) - this.getCardType(b);
            };
        });
        let cards = [];
        for(let i = 0 ;i < handCard.length -2;i ++){
            if(handCard[i] === handCard[i + 1] && handCard[i + 1] === handCard[i + 2] &&  handCard[i] === card){
                cards.push({
                    type:YCMJ_OP_TYPE.MING_GANG,
                    cards:handCard[i]
                });
                i += 2;
                break;
            };
        };
        return cards;
    }else
    {
        return [];
    }
}
MJLogic.prototype.agang = function(handCard,card){
    if(handCard instanceof Array &&  handCard.length >= 2 ){
        handCard.sort((a,b)=>{
            if(this.getCardType(a) == this.getCardType(b)){
                return this.getCardValue(a) - this.getCardValue(b);
            }else{
                return this.getCardType(a) - this.getCardType(b);
            };
        });
        let cards = [];
        for(let i = 0 ;i < handCard.length - 2;i ++){
            if(handCard[i] === handCard[i + 1] && handCard[i + 1] === handCard[i + 2] &&  handCard[i] === card){
                cards.push({
                    type:YCMJ_OP_TYPE.AN_GANG,
                    cards:handCard[i]
                });
                i += 2;
                break;
            };
        };
        return cards;
    }else
    {
        return [];
    }
}
MJLogic.prototype.pgang = function(pgCard,card){
    if(pgCard instanceof Array &&  pgCard.length > 0 ){
        pgCard.sort((a,b)=>{
            if(this.getCardType(a) == this.getCardType(b)){
                return this.getCardValue(a) - this.getCardValue(b);
            }else{
                return this.getCardType(a) - this.getCardType(b);
            };
        });
        let cards = [];
        for(let i=0;i<pgCard.length;i++){
            let p = pgCard[i];
            if(p instanceof Array && p.length == 3){
                if(p[0] === card){
                    cards.push({
                        type:YCMJ_OP_TYPE.PENG_GANG,
                        cards:p[0]
                    });
                    break;
                };
            };
        };
        return cards;
    }else
    {
        return [];
    }
}
MJLogic.prototype.qixing = function(cards){
    if(cards instanceof Array && cards.length == 14){
        // for(let i = cards.length -1 ;i >= 0; i--){

        // };
    }else
    {
        return false;
    }
}
MJLogic.prototype.shisanyao = function(cards){
    if(cards instanceof Array && cards.length == 14){
        let index = 0;
        for(let i = 0;i < cards.length - 5 ; i +=3){
            let card = cards.slice(i,i+3);
            let type = i / 3 + 1;
            if(!this.isBtTwo(card,type)){
                return false;
            };
            index = i+3;
        };
        for(let i = index ;i < cards.length - 1;i++){
            let card = cards[i];
            if(this.getCardType(card) < 4){
                return false;
            };
            let cardNext = cards[i + 1];
            if(this.getCardType(cardNext) < 4){
                return false;
            };
            if(card == cardNext){
                return false;
            };
        };
        return true;
    };
    return false;
}
MJLogic.prototype.hu = function(handCard,card){
    let cards = handCard.concat(card);  
    cards.sort((a,b)=>{
        if(this.getCardType(a) == this.getCardType(b)){
            return this.getCardValue(a) - this.getCardValue(b);
        }else{
            return this.getCardType(a) - this.getCardType(b);
        }
    });
    // let isqixing = this.qixing(cards);
    // if(isqixing){
    //     return {
    //         type:YCMJ_HUPAI_TYPE.QIXING,
    //         huCard:card
    //     };
    // };
    let isshisanyao = this.shisanyao(cards);
    if(isshisanyao){
        return {
            type:YCMJ_HUPAI_TYPE.SHISANYAO,
            huCard:card
        };
    };
    let isPinghu = this.pinghu(cards);
    if(isPinghu){
        return {
            type:YCMJ_HUPAI_TYPE.PINGHU,
            huCard:card
        }
    }
    return null;
}
module.exports = function(){
    if(!this._mjlogicinstance){
        this._mjlogicinstance = new MJLogic();
    };
    return this._mjlogicinstance;
};
